<?php
namespace app\api\controller\zfcom;
class Article extends Base
{
    //列表
    public function lists()
    {
        $d = $this->post;
        $rule = [
            'page'=>'require|integer',
            'limit'=>'require|integer',
        ];
        $msg = [];
        check($rule,$msg,$d);

        $map = [];
        if($d['cat_id']!=''){
            if($d['type']==0){
                $map[] = ['and','cat_id','=',$d['cat_id']];
            }else{
                $ids = [$d['cat_id']];
                //下级分类id,放入$ids
                $map[] = ['and','cat_id','in',$ids];
            }
        }
        if($d['keyword']!=''){
            $map[] = ['and','title','like',"%{$d['keyword']}%"];
        }
    	$w = where($map);
        $rs = $this->db->query("select id,title,keyword,description,pic,content,views,create_time from article $w[0] order by sort desc,id desc".limit($d['page'],$d['limit']), $w[1],'',true);
        foreach ($rs[0] as $k => $v) {
            $rs[0][$k]['h5_url'] = url('h5/articledetail',['id'=>$v['id']]);
        }
       	if($rs){
            return suc($rs[0], $rs[1]);
        }
        return suc([],0);
    }

    public function detail(){
        $id = $this->post("id");
        $rs = $this->db->query("select id,title,keyword,description,pic,content,views,create_time from article where id = ?", [$id],1);
       	if($rs){
            $rs['h5_url'] = url('h5/articledetail',['id'=>$rs['id']]);
            return suc(['info'=>$rs]);
        }
        return suc([],0);
    }
}
